﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ChessGameV2.Scenes
{
    public class PlayScene
    {
        #region properties
        SpriteBatch sp; // Bien engine
        bool isSound = false;
        //bool SettingisMouseOver = false;
        Texture2D picBackground;                         //Hinh Background
        Texture2D picBoard;                             //Hinh ban co
        Texture2D[] pieces = new Texture2D[18];     //Tap hinh quan co
        Texture2D Selected;
        Texture2D ButSetting;
        Vector2 posboard;
        public Vector2 butSetting;
        SpriteFont Timeout;
        SpriteFont TimeTitle;
        public Vector2 posBoard;
        Texture2D Loading;
        Vector2 posLoading;
        Vector2[] posPiece;
        Texture2D StateWin;
        Texture2D StateDraw;
        Texture2D StateLost;
        Vector2 posState;
        //
        Texture2D CastleKingSide; //nhap thanh
        Texture2D PawnPromotion; //phong hau
        Texture2D Checkmate; //chieu tuong
        Texture2D ScreenSetting;
        Vector2 posCastleKinh_PawnPromotion_Checkmate;
        Vector2 posScreenSetting;

        #endregion
        public PlayScene(SpriteBatch _sp)
        {
            this.sp = _sp;
            posboard = new Vector2(16, 59);
            posBoard = new Vector2(49, 92);
            butSetting = new Vector2(450, 5);
            posScreenSetting = new Vector2(300, 50);
            posLoading = new Vector2(posBoard.X+50, posBoard.Y + 100);
            posCastleKinh_PawnPromotion_Checkmate = new Vector2(posBoard.X + 50, posBoard.Y + 100);
            posState = new Vector2(55, 200);
            posPiece = new Vector2[120];
            for (int i = 21; i < 99; i++)
            {
                int detaX = ((i-1) % 10) * 50;
                int detaY = ((i-21) / 10) * 50;
                posPiece[i] = new Vector2(posBoard.X + detaX, posBoard.Y + detaY);
                if (i % 10 == 8) i += 2;
            }
        }
        #region LOAD BOARD
        public void LoadContent(ContentManager Content)
        {
            picBackground = Content.Load<Texture2D>("GBoard/Background");    //Load Background
            picBoard = Content.Load<Texture2D>("GBoard/board");    //Load Background
            ButSetting = Content.Load<Texture2D>("GBoard/Button/ButtonSetting");    //Load Background

            pieces[1] = Content.Load<Texture2D>("GBoard/Pieces/White/pawn");
            pieces[2] = Content.Load<Texture2D>("GBoard/Pieces/White/knight");
            pieces[3] = Content.Load<Texture2D>("GBoard/Pieces/White/bishop");
            pieces[4] = Content.Load<Texture2D>("GBoard/Pieces/White/rook");
            pieces[5] = Content.Load<Texture2D>("GBoard/Pieces/White/queen");
            pieces[6] = Content.Load<Texture2D>("GBoard/Pieces/White/king");

            pieces[11] = Content.Load<Texture2D>("GBoard/Pieces/Black/pawn");
            pieces[12] = Content.Load<Texture2D>("GBoard/Pieces/Black/knight");
            pieces[13] = Content.Load<Texture2D>("GBoard/Pieces/Black/bishop");
            pieces[14] = Content.Load<Texture2D>("GBoard/Pieces/Black/rook");
            pieces[15] = Content.Load<Texture2D>("GBoard/Pieces/Black/queen");
            pieces[16] = Content.Load<Texture2D>("GBoard/Pieces/Black/king");

            Timeout = Content.Load<SpriteFont>("Gboard/Time");
            TimeTitle = Content.Load<SpriteFont>("Gboard/timetitle");

            Selected = Content.Load<Texture2D>("GBoard/Selected");
            Loading = Content.Load<Texture2D>("GBoard/Loading");

            StateWin = Content.Load<Texture2D>("GBoard/State/win");
            StateDraw = Content.Load<Texture2D>("GBoard/State/Draw");
            StateLost = Content.Load<Texture2D>("GBoard/State/lose");

            //
            Checkmate = Content.Load<Texture2D>("GBoard/State/chieu-tuong");
            CastleKingSide = Content.Load<Texture2D>("GBoard/State/nhap-thanh");
            PawnPromotion = Content.Load<Texture2D>("GBoard/State/phong-hau");
            ScreenSetting = Content.Load<Texture2D>("GBoard/Button/setting");
        }
        #endregion
        #region Update
        public void update()
        {

            if (Game1.currentMouseState.X > butSetting.X && Game1.currentMouseState.Y > butSetting.Y && Game1.currentMouseState.Y < butSetting.Y + 40)
            {
                if (isSound) Sounds.Move();
                isSound = false;
                //this.SettingisMouseOver = true;
                if (Game1.lastMouseState.LeftButton == ButtonState.Released && Game1.currentMouseState.LeftButton == ButtonState.Pressed)
                {
                    Game1.gameScene = GameScene.Setting;
                        sp.Draw(ScreenSetting, posScreenSetting, Color.White);
                }
            }
            else
            {
                isSound = true;
                //this.SettingisMouseOver = false;
            }
           
        }
        public void DrawSetting()
        {
            sp.Draw(ScreenSetting, posScreenSetting, Color.White);
        }
        public int getPieceCell()
        {
            if (Game1.currentMouseState.X >= posBoard.X && Game1.currentMouseState.Y >= posBoard.Y && Game1.currentMouseState.X <= posBoard.X + 400 && Game1.currentMouseState.Y <= posBoard.Y + 400)
            {
                if (Game1.lastMouseState.LeftButton == ButtonState.Released && Game1.currentMouseState.LeftButton == ButtonState.Pressed)
                {
                    int x = (Game1.currentMouseState.X - (int)posBoard.X) / 50; // Toa do X (theo ban co)
                    int y = (Game1.currentMouseState.Y - (int)posBoard.Y) / 50; // Toa do Y (theo ban co)
                    if (x >= 0 && x <= 7 && y >= 0 && y <= 7)
                    {
                       return 21 + y * 10 + x;
                    }
                }
            }
            return int.MaxValue;
        }
        #endregion
        #region DRAW BOARD
        public void DrawState(Player player)
        {
            if (player.playerState == PlayerState.Draw)
                sp.Draw(StateDraw, posState, Color.White);

            if (player.playerState == PlayerState.Lost)
            {
                if (player.type == PlayerType.Human)
                    sp.Draw(StateLost, posState, Color.White);
                else
                    sp.Draw(StateWin, posState, Color.White);
            }
            return;
        }
        public void DrawBoard()
        {
            sp.Draw(picBackground, Vector2.Zero, Color.White); // ve ban co
            sp.Draw(picBoard, posboard, Color.White); // ve ban co
            sp.Draw(ButSetting, butSetting, Color.White); // ve ban co
        }
        public void DrawSlowPieces(int[] board,int slow)
        {
            if (slow > 99) slow = 99;
            if (board != null)
            {
                for (int i = 21; i < slow; i++)
                {
                    paintField(i, board);
                    if (i % 10 == 8)
                        i += 2;
                }
                sp.Draw(Loading,posLoading,Color.White);
            }
        }
        public void showTime(int Time)
        {
            Color c = Color.White;
            string s = (Time / 1000 % 60).ToString();
            if (Time/1000 % 60 < 10)
            {
                s = "0" + s;
            }
            if (Time < 10000)
            {
                c = Color.Red;
                if (Time % 1000 == 0) Sounds.Timeless();
            }
            string m = ((Time/60000)).ToString();

            if(Time < 600000) m = "0"+m;
            string time = string.Format("{0} : {1}",m,s);
            sp.DrawString(Timeout, time, new Vector2(212,47), c);
            sp.DrawString(TimeTitle, "Time", new Vector2(235, 73), Color.Yellow);
        }
        public void DrawSelectedCell(int index)
        {
            try
            {
                sp.Draw(Selected, posPiece[index], Color.White);
            }
            catch { }
        }
        public void DrawSelectedCells(Move mselected, List<Move> genMove)
        {
            if (mselected != null && mselected.Start != int.MinValue)
            {
                DrawSelectedCell(mselected.Start);
                if(genMove != null)
                 foreach (Move m in genMove)
                    {
                        if(m.Start == mselected.Start && m.End != int.MinValue)
                            DrawSelectedCell(m.End);    
                    }
            }
        }
        public void DrawPieces(int[] board)
        {
            if (board != null)
            {
                for (int i = 21; i < 99; i++)
                {
                    paintField(i, board);
                    if (i % 10 == 8)
                        i += 2;
                }
            }
        }
        private int getIdPieces(int index, int[] board)
        {
            return board[index] % 100 - 10;
        }
        private void paintField(int i, int[] board)
        {
            int pi = getIdPieces(i, board);
            if((pi>0 && pi <=6) || (pi>10 && pi <=16)) 
                sp.Draw(pieces[pi], posPiece[i], Color.White);
        }
        #endregion

    }
}
